#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define ERROR -999

typedef int Position;
typedef int ElementType;
typedef struct SNode* PtrToSNode;
struct SNode
{
    ElementType* Data; // 存储元素的数组，之前也用ElementType Data[MAXSIZE];
    Position Top; // 栈顶指针，指向上面数组的最后一个元素位置
    int MaxSize;  // 栈最大容量
};
typedef PtrToSNode Stack;

Stack CreateStack(int MaxSize)
{
    Stack S = (Stack) malloc(sizeof(struct SNode));
    S->Data = (ElementType*) malloc(MaxSize * sizeof(ElementType));
    S->MaxSize = MaxSize;
    S->Top = -1;
    return S;
}

bool IsFull(Stack S) 
{
    return (S->Top == S->MaxSize - 1);
}

bool IsEmpty(Stack S)
{
    return (S->Top == -1);
}

bool Push(Stack S, ElementType X) 
{
    if (IsFull(S)) {
        printf("堆栈满\n");
        return false;
    }
    S->Data[++(S->Top)] = X;
    return true;
}

ElementType Pop(Stack S)
{
    if (IsEmpty(S)) {
        printf("堆栈空\n");
        return ERROR;
    }
    return S->Data[(S->Top)--];
}

void PrintStack(Stack S)
{
    for (int i = 0; i <= S->Top; i++) {
        printf("%d ", S->Data[i]);
        printf("\n");
    }
}

int main()
{
    Stack S = CreateStack(10);
    Push(S, 1);
    Push(S, 2);
    Push(S, 3);
    Push(S, 4);
    Push(S, 5);
    Pop(S);
    ElementType result = Pop(S);
    printf("popped result is %d\n", result);
    PrintStack(S);
    return 0;
}